package com.hx.vending.controller;

import com.hx.vending.annotation.Authorization;
import com.hx.vending.annotation.CurrentUserId;
import com.hx.vending.bean.Model;
import com.hx.vending.mqtt.RunnerSendMqtt;
import com.hx.vending.service.IModelService;
import com.hx.vending.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by Administrator on 2018/11/28.
 * 售货机模板
 */
@Controller
@RequestMapping("/model")
public class ModelController {

    @Autowired
    private IModelService iModelService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 添加模板信息
     * @param userId           商户id
     * @param model            模板信息
     * @return
     */
    @RequestMapping(value = "/insertModel", method = RequestMethod.POST)
    @ResponseBody
    @Authorization
    public Msg insertModel(@CurrentUserId Integer userId, Model model){

        return this.iModelService.insertModel(userId, model);
    }


    /**
     * 修改模板信息
     * @param model
     * @return
     */
    @RequestMapping(value = "/updateModel", method = RequestMethod.POST)
    @ResponseBody
    public Msg updateModel(Model model){

        return this.iModelService.updateModel(model);
    }


    /**
     * 删除模板信息
     * @param modelId 模板id
     * @return Msg
     */
    @RequestMapping(value = "/deleteModel", method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteModel(Integer modelId){

        return this.iModelService.deleteModel(modelId);
    }


    /**
     * 查询模板/设备所有货道信息
     * @param model
     * @return
     */
    @RequestMapping(value = "/selectAllModelOrEquiMessage", method = RequestMethod.GET)
    @ResponseBody
    public Msg selectAllModelOrEquiMessage(Model model, String equipmentId){

        return this.iModelService.selectAllModelOrEquiMessage(model, equipmentId);
    }


    /**
     * 查询该用户下的模板列表
     * @param userId        用户id
     * @param pn            页码
     * @param model         模板查询条件
     * @return
     */
    @GetMapping("/selectModelList")
    @ResponseBody
    @Authorization
    public Msg selectModelList(@CurrentUserId Integer userId, @RequestParam(value = "pn", defaultValue = "1")Integer pn, Model model, @RequestParam(value = "equipmentId", required = false)String equipmentId) {
        return this.iModelService.selectModelList(userId, pn, model, equipmentId);
    }


    /**
     * 将模板应用到设备
     * @param modelId
     * @param equipmentId
     * @return
     */
    @PostMapping("/insertEquipmentByModel")
    @ResponseBody
    public Msg insertEquipmentByModel(Integer modelId, String equipmentId){
        return this.iModelService.insertEquipmentByModel(modelId, equipmentId);
    }


    @PostMapping("/mqtt")
    @ResponseBody
    public void mqtt(){
        //redisTemplate.boundValueOps("").set(, Constants.MQTT_EXPIRES_HOUR, TimeUnit.SECONDS);

        RunnerSendMqtt runnerSendMqtt = new RunnerSendMqtt();
        runnerSendMqtt.setMessage("测试");
        runnerSendMqtt.setRedisTemplate(redisTemplate);
        runnerSendMqtt.setTheme("aa");
        runnerSendMqtt.setThreadName("测试");
        runnerSendMqtt.setRedisKey("mqtt");
        runnerSendMqtt.start();
    }

}
